import axios from 'axios'
import router from '@/router'
import store from '@/store'

const request = axios.create({
  baseURL: 'http://124.223.14.236:8060/home'
})

// 添加请求拦截器
request.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    config.headers.Authorization = 'Bearer ' + store.state.youdianda_token
    return config
  },
  error => Promise.reject(error)
)

// 添加响应拦截器
request.interceptors.response.use(
  response => response.data,
  function (error) {
    if (error.response.status === 401) {
      store.commit('updateToken', '')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

export default request
